package com.hh.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hh.constant.MenuConstant;
import com.hh.entity.SysMenu;
import com.hh.mapper.SysMenuMapper;
import com.hh.service.SysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单管理 服务实现类
 * </p>
 *
 * @author 辉辉
 * @since 2022-08-15
 */
@Service
@Slf4j
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    @Resource
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 加载菜单
     * @param userId
     * @return
     */
    @Override
    public List<SysMenu> loadUserMenu(String userId) {
        log.info("加载树菜单");
        // 从缓存里拿
        String menuStr = redisTemplate.opsForValue().get(MenuConstant.MENU_PREFIX + userId);
        List<SysMenu> sysMenus = null;
        if (StringUtils.isEmpty(menuStr)) {
            // 没有查数据库 放入缓存
            sysMenus = sysMenuMapper.findMenuByUserId(Long.valueOf(userId));
            if (CollectionUtils.isEmpty(sysMenus)) {
                // 说明该用户没有菜单
                return Collections.emptyList();
            }
            // 转一个json串存入redis
            redisTemplate.opsForValue().set(MenuConstant.MENU_PREFIX + userId, JSON.toJSONString(sysMenus), Duration.ofDays(7));
        } else {
            sysMenus = JSON.parseArray(menuStr, SysMenu.class);
        }
        // 如过缓存有 就直接用
        // 循环组装树菜单
        return loadMenuTree(sysMenus, 0L);
    }

    /**
     * 组装树菜单
     * @param sysMenus
     * @param pid
     * @return
     */
    private List<SysMenu> loadMenuTree(List<SysMenu> sysMenus, Long pid) {
//        // 1. 拿到顶级菜单
        List<SysMenu> root = sysMenus.stream().filter(sysMenu -> sysMenu.getParentId().equals(pid)).collect(Collectors.toList());
        // 循环root
        root.stream().forEach(r -> {
            // 谁的父id等于它的id
            List<SysMenu> child = sysMenus.stream().filter(sysMenu -> sysMenu.getParentId().equals(r.getMenuId())).collect(Collectors.toList());
            r.setList(child);
        });
//        List<SysMenu> level1 = new ArrayList<>();
//        for (SysMenu sysMenu : sysMenus) {
//            if (sysMenu.getParentId() == 0) {
//                level1.add(sysMenu);
//            }
//        }
//        List<SysMenu> root = new ArrayList<>(level1);
//        for (SysMenu sysMenu : root) {
//            if (sysMenu.getParentId() == 0) {
//                List<SysMenu> child = new ArrayList<>();
//                for (SysMenu menu : sysMenus) {
//                    if (sysMenu.getMenuId().equals(menu.getParentId())) {
//                        child.add(menu);
//                    }
//                }
//                sysMenu.setList(child);
//            }
//        }
        return root;
    }
}
